package com.xk._01基础篇._07AVL.tree;

import com.xk._01基础篇._06二叉搜索树02.printer.BinaryTreeInfo;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @description:
 * @author: xu
 * @date: 2022/9/21 16:33
 */
public class BinaryTree<E> implements BinaryTreeInfo {
    protected int size;
    protected Node<E> root; // 根结点

    protected static class Node<E> {
        E elememt;
        Node<E> parent; // 父结点
        Node<E> left; // 左结点
        Node<E> right; // 右结点
        public Node(E elememt, Node<E> parent) {
            this.elememt = elememt;
            this.parent = parent;
        }
        public boolean isLeaf(){ // 是否是叶子节点
            return left == null && right == null;
        }
        public boolean hasTwoChildren() { // 是否有两个子节点
            return left != null && right != null;
        }
        public boolean isLeftChild(){ // 判断自己是不是左子树
            return parent!=null && this==parent.left;
        }
        public boolean isRightChild(){ // 判断自己是不是右子树
            return parent!=null && this==parent.right;
        }
        // 返回兄弟节点
        public Node<E> sibling() { // 红黑树中用到, 返回兄弟节点
            if (isLeftChild()) return parent.right;
            if (isRightChild()) return parent.left;
            return null;
        }
    }

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    public void clear(){
        root = null;
        size = 0;
    }

    /**
     * 计算树的高度
     * @return
     */
    public int height(){
        return height(root);
    }
    private int height(Node<E> node){
        // 非递归
        if (node == null) return 0;
        int height = 0; // 树的高度
        int levelSize = 1; // 存储着每一层的元素数量
        Queue<Node<E>> queue = new LinkedList<>();
        queue.offer(node);
        while (!queue.isEmpty()){
            Node<E> poll = queue.poll();
            levelSize--;
            if (poll.left != null)  queue.offer(poll.left);
            if (poll.right != null) queue.offer(poll.right);
            if (levelSize == 0) { // 意味着即将访问下一层
                levelSize = queue.size();
                height++;
            }
        }
        return height;
    }

    /**
     * 判断此树是否是完全二叉树 层序遍历
     * @return
     */
    public boolean isComplete(){
        if (root == null) return false;
        Queue<Node<E>> queue = new LinkedList<>();
        queue.offer(root);

        boolean leaf = false;
        while (!queue.isEmpty()){
            Node<E> node = queue.poll();
            if (leaf && !node.isLeaf()) return false;

            if (node.left != null){
                queue.offer(node.left);
            } else if (node.right != null) {
                // poll.left == null && poll.right != null
                return false;
            }

            if (node.right != null) {
                queue.offer(node.right);
            } else {
                // poll.left == null && poll.right == null
                // poll.left != null && poll.right == null
                leaf = true;
            }
        }
        return true;
    }

    /**
     * 前序遍历
     */
    public void preOrder(Visitor<E> visitor){
        preOrder(root, visitor);
    }
    private void preOrder(Node<E> root, Visitor<E> visitor){
        /*
        if(root == null || visitor == null) return;
        visitor.visit(root.elememt);
        preOrder(root.left, visitor);
        preOrder(root.right, visitor);
        */

        // 非递归 时间复杂度 O(n) 空间复杂度 O(n)
        /*
        if(root == null || visitor == null) return;
        Node<E> node = root;
        Stack<Node<E>> stack = new Stack<>();
        while (true) {
            if (node != null){
                // 访问 node 节点
                visitor.visit(node.elememt);
                // 将右子节点入栈
                if (node.right != null) stack.push(node.right);
                // 向左走
                node = node.left;
            } else {
                if (stack.isEmpty()) return;
                //处理右边
                node = stack.pop();
            }
        }
        */

        if(root == null || visitor == null) return;
        Stack<Node<E>> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            Node<E> node = stack.pop();
            visitor.visit(node.elememt);
            if (node.right != null) stack.push(node.right);
            if (node.left != null) stack.push(node.left);
        }

    }

    /**
     * 中序遍历
     */
    public void inOrder(Visitor<E> visitor){
        inOrder(root, visitor);
    }
    private void inOrder(Node<E> root, Visitor<E> visitor){
        /*
        if(root == null || visitor == null) return;
        inOrder(root.left, visitor);
        visitor.visit(root.elememt);
        inOrder(root.right, visitor);
        */

        // 非递归
        if(root == null || visitor == null) return;
        Node<E> node = root;
        Stack<Node<E>> stack = new Stack<>();
        while (true) {
            if (node != null){
                stack.push(node);
                // 往左走
                node = node.left;
            } else {
                if (stack.isEmpty()) return;
                //处理右边
                node = stack.pop();
                visitor.visit(node.elememt);

                node = node.right;
            }
        }
    }

    /**
     * 后序遍历
     */
    public void postOrder(Visitor<E> visitor){
        postOrder(root, visitor);
    }
    private void postOrder(Node<E> root, Visitor<E> visitor){
        /*
        if(root == null || visitor == null) return;
        postOrder(root.left, visitor);
        postOrder(root.right, visitor);
        visitor.visit(root.elememt);
        */

        // 非递归
        if(root == null || visitor == null) return;
        // 记录上一次弹出访问的节点
        Node<E> prev = null;
        Stack<Node<E>> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node<E> top = stack.peek();
            if (top.isLeaf() || (prev != null && prev.parent == top)) {
                prev = stack.pop();
                // 访问节点
                visitor.visit(prev.elememt);
            } else {
                if (top.right != null) {
                    stack.push(top.right);
                }
                if (top.left != null) {
                    stack.push(top.left);
                }
            }
        }
    }

    /**
     * 层序遍历
     */
    public void levelOrder(Visitor<E> visitor){
        if (root == null || visitor == null) return;
        Node<E> node = root;
        Queue<Node<E>> queue = new LinkedList<>();
        queue.offer(node);
        while (!queue.isEmpty()){
            Node<E> poll = queue.poll();
            visitor.visit(poll.elememt);
            if (poll.left != null) queue.offer(poll.left);
            if (poll.right != null) queue.offer(poll.right);
        }
    }

    public static interface Visitor<E> {
        void visit(E element);
    }

    /**
     * 获取指定结点的前驱结点
     * @param node
     * @return
     */
    protected Node<E> predecessor(Node<E> node) {
        if (node == null) return null;

        // 前驱结点在左子树当中(left.right.right.right......)
        Node<E> p = node.left;
        if (p != null) {
            while (p.right != null){
                p = p.right;
            }
            return p;
        }

        // 从祖父结点中寻找前驱结点
        while (node.parent != null && node == node.parent.left){
            node = node.parent;
        }
        // node.parent == null
        // node == node.parent.right
        return node.parent;
    }

    /**
     * 获取指定结点的后继结点
     * @param node
     * @return
     */
    protected Node<E> successor(Node<E> node) {
        if (node == null) return null;

        // 前驱结点在右子树当中(right.left.left.left.....)
        Node<E> p = node.right;
        if (p != null) {
            while (p.left != null){
                p = p.left;
            }
            return p;
        }

        // 从祖父结点中寻找前驱结点
        while (node.parent != null && node == node.parent.right){
            node = node.parent;
        }
        // node.parent == null
        // node == node.parent.left
        return node.parent;
    }

    protected Node<E> createNode(E element, Node<E> parent) {
        return new Node<>(element, parent);
    }

    @Override
    public Object root() {
        return root;
    }

    @Override
    public Object left(Object node) {
        return ((Node<E>) node).left;
    }

    @Override
    public Object right(Object node) {
        return ((Node<E>) node).right;
    }

    @Override
    public Object string(Object node) {
        Node<E> myNode = (Node<E>) node;
        String parentStr = "null";
        if (myNode.parent != null){
            parentStr = myNode.parent.elememt.toString();
        }
        return ((Node<E>) node).elememt + "_p(" + parentStr + ")";
    }
}
